package org.csu.mypetstorebackstage.controller;

import org.csu.mypetstorebackstage.domain.Item;
import org.csu.mypetstorebackstage.domain.Product;
import org.csu.mypetstorebackstage.persistence.ProductMapper;
import org.csu.mypetstorebackstage.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    ItemService itemService;
    @Autowired
    ProductMapper productMapper;
    @GetMapping("/getItemView")
    public String getItemView(Model model, String productId,HttpSession session)
    {
        List<Item> itemList=itemService.getItemAllByProduct(productId);
        session.setAttribute("itemList",itemList);
        session.setAttribute("productId",productId);
        return "category/item";
    }

    @PostMapping("/getItemSpecial")
    public String getItemSpecial(Model model,String itemId)
    {
        Item item = itemService.getItemById(itemId);
        List<Item> itemList = new ArrayList<>();
        itemList.add(item);
        model.addAttribute("itemList",itemList);
        return "category/item";
    }

    @GetMapping("getItemInfo")
    public String getItemInfo(Model model,String itemId)
    {
        Item item = itemService.getItemById(itemId);
        System.out.println(itemId);
        model.addAttribute("item",item);
        return "category/itemInfo";
    }

    @GetMapping("editItem")
    public String editItem(Model model,String itemId)
    {
        Item item = itemService.getItemById(itemId);
        model.addAttribute("item",item);
        System.out.println(itemId);
        return "category/itemEdit";
    }

    @PostMapping("editItemConfirm")
    public String editItemConfirm(Model model,BigDecimal listPrice, BigDecimal unitCost , String status, String itemId, String productId)
    {
        Item item = new Item();
        item.setItemId(itemId);
        item.setListPrice(listPrice);
        item.setUnitCost(unitCost);
        item.setStatus(status);

        itemService.editItem(item);
        model.addAttribute("item",itemService.getItemById(itemId));

        return "category/itemInfo";
    }


    @GetMapping("/toaddItem")
    public String toaddItem()
    {
        return "/category/addItem";
    }


    @PostMapping("/addItem")
    public String addItem(Item item, Model model, HttpSession session)
    {
        Boolean isInsert=productMapper.addItem(item);
        if(isInsert==true)
        {
            model.addAttribute("Message","已加入成功");
            session.setAttribute("itemList",itemService.getItemListByProduct(item.getProductId()));;
            return "/category/item";
        }
        else
        {
            model.addAttribute("Message","Exist");
            return "/category/addItem";
        }
    }

    @GetMapping("deleteItem")
    public String deleteItem(String itemId,Item item,HttpSession session,int delete)
    {
//        String productId=item.getProductId();
        System.out.println(itemId+"ssss");
        Product product=productMapper.getProduct(itemService.getItemById(itemId).getProductId());

        if(delete==3)
        {
            itemService.deleteItemByItemId(item.getItemId());
        }
        List<Item>itemList=productMapper.getItemsByProductId(product.getProductId());
        session.setAttribute("itemList",itemList);




//        session.setAttribute("categoryName",categoryName);
//        System.out.println("删除成功");
        return "/category/item";
    }
}
